package com.general.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.general.template.enums.ResultEnum;
import com.general.template.pojo.PowerDimension;
import com.general.template.service.PowerDimensionService;
import com.general.template.utils.Pages;
import com.general.template.utils.Result;
import com.general.template.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 杰哥
 * Date: 2021/2/16 18:08
 * @Description:
 **/
@RequestMapping("dimension")
@RestController
@Api(value="不同岗位维度管理",description = "对不同岗位维度管理的查看和修改")
public class DimensionController {

    @Autowired
    private PowerDimensionService powerDimensionService;

    /**
     * 添加维度
     *
     * @author 杰哥
     * @date 2021/2/16 18:17
     * @param dimension
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value="add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="添加维度",notes = "添加维度")
    public Result<Object> add(@RequestBody PowerDimension dimension){
        boolean save = this.powerDimensionService.save(dimension);
        if (save){
            return new Result<>(ResultEnum.SUCCESS,"添加成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"添加失败!");
        }
    }

    /**
     * 修改维度信息
     *
     * @author 杰哥
     * @date 2021/2/16 18:18
     * @param dimension
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value="update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改维度",notes = "修改维度")
    public Result<Object> addJob(@RequestBody PowerDimension dimension){
        boolean save = this.powerDimensionService.updateById(dimension);
        if (save){
            return new Result<>(ResultEnum.SUCCESS,"修改成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"修改失败!");
        }
    }

    /**
     * 禁用岗位
     *
     * @author 杰哥
     * @date 2021/2/16 18:22
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value="delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="禁用岗位",notes = "禁用岗位")
    public Result<Object> delete(@PathVariable("id") Integer id){
        PowerDimension dimension = new PowerDimension();
        dimension.setId(id);
        dimension.setStatus(1);
        boolean b = this.powerDimensionService.updateById(dimension);
        if (b){
            return new Result<>(ResultEnum.SUCCESS,"禁用成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"禁用失败!");
        }
    }

    /**
     * 启用岗位
     *
     * @author 杰哥
     * @date 2021/2/16 18:22
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value="enable/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="启用岗位",notes = "启用岗位")
    public Result<Object> enable(@PathVariable("id") Integer id){
        PowerDimension dimension = new PowerDimension();
        dimension.setId(id);
        dimension.setStatus(0);
        boolean b = this.powerDimensionService.updateById(dimension);
        if (b){
            return new Result<>(ResultEnum.SUCCESS,"启用成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"启用失败!");
        }
    }

    /**
     * 分页查询
     *
     * @author 杰哥
     * @date 2021/2/16 18:34
     * @param pages
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value="pageList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="查询维度",notes = "查询维度")
    public Result<Object> pageList(@RequestBody Pages<PowerDimension> pages){
        Pages list = this.powerDimensionService.pageList(pages);
        return new Result<>(ResultEnum.SUCCESS,list);
    }

    @RequestMapping(value="queryList/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="根据职业查询维度",notes = "根据职业查询维度")
    public Result<Object> queryList(@PathVariable("id") Integer id){
        if (StringUtils.isNullOrEmpty(id)){
            return new Result<>(ResultEnum.SUCCESS,null);
        }
        List<PowerDimension> list = this.powerDimensionService.list(new QueryWrapper<PowerDimension>().eq("jobs_id", id).eq("status",0));
        return new Result<>(ResultEnum.SUCCESS,list);
    }

    @RequestMapping(value="getList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="根据职业查询维度",notes = "根据职业查询维度")
    public Result<Object> getList(){
        List<PowerDimension> list = this.powerDimensionService.list(new QueryWrapper<PowerDimension>().eq("status",0));
        return new Result<>(ResultEnum.SUCCESS,list);
    }

}
